package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.web.domain.DeviceAccessCode;
import com.ruoyi.web.domain.DeviceAccessVisitor;
import com.ruoyi.web.domain.dto.DeviceAccessCodeDto;
import com.ruoyi.web.domain.dto.DeviceAccessVisitorDto;
import com.ruoyi.web.service.IDeviceAccessCodeService;
import com.ruoyi.web.service.IDeviceAccessVisitorService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@Api(tags = "访客管理控制层")
@RequestMapping("/accessVisitor")
public class DeviceAccessVisitorController extends BaseController {

    @Autowired
    private IDeviceAccessVisitorService accessVisitorService;


    @GetMapping("/code/page")
    public TableDataInfo pageByCode(DeviceAccessVisitor condition)
    {
        startPage();
        List<DeviceAccessVisitor> deviceAccessCodes = accessVisitorService.selectByCondition(condition);
        return getDataTable(deviceAccessCodes);
    }

    @GetMapping("/page")
    public TableDataInfo list(DeviceAccessVisitor condition)
    {
        startPage();
        List<DeviceAccessVisitor> deviceAccessCodes = accessVisitorService.selectDistinctByCode(condition);
        return getDataTable(deviceAccessCodes);
    }






    @PostMapping("/recordVisitor")
    public AjaxResult recordVisitor(@RequestBody DeviceAccessVisitor visitor, HttpServletRequest request)
    {

        String ip = IpUtils.getIpAddr(request);
        logger.info("访客ip为{}", ip);
        if(StringUtils.isEmpty(visitor.getAccessCode())){
            return AjaxResult.warn("访问码不能为空");
        }
        visitor.setIpAddress(ip);
        accessVisitorService.recordVisitor(visitor);
        return AjaxResult.success();
    }




    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam("baseId") Long baseId)
    {
        DeviceAccessVisitor detail = accessVisitorService.detail(baseId);
        return AjaxResult.success(detail);
    }

    /**
     * 用户数量
     * @return
     */
    @GetMapping("/visitorCount")
    public AjaxResult visitorCount()
    {
        Map map = accessVisitorService.visitorCount();
        return AjaxResult.success(map != null ? map : new HashMap());
    }

    /**
     * 设备访问次数
     * @return
     */
    @GetMapping("/deviceCount")
    public AjaxResult deviceCount()
    {
        List<Map> list = accessVisitorService.deviceCount();
        return AjaxResult.success(list != null ? list : new ArrayList());
    }

    /**
     * 设备访问时长
     * @return
     */
    @GetMapping("/deviceAccessInterval")
    public AjaxResult deviceAccessInterval()
    {
        List<Map> list = accessVisitorService.deviceAccessInterval();
        return AjaxResult.success(list != null ? list : new ArrayList());
    }

    /**
     * 访问趋势
     * @param dto
     * @return
     */
    @GetMapping("/deviceAccessTrend")
    public AjaxResult deviceAccessTrend(DeviceAccessVisitorDto dto)
    {
        String type = dto.getType();
        if(StringUtils.containsAny(type, "00", "01", "02", "03", "04")){
            List<Map> list = accessVisitorService.deviceAccessTrend(dto);
            return AjaxResult.success(list != null ? list : new ArrayList());
        }else{
            return AjaxResult.warn("请选择统计类型");
        }


    }


}
